<template>

  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryParams"
      class="-mb-15px"
      label-width="100px"
    >

      <el-form-item label="实验室编号" prop="stuId">
        <el-input
          v-model.number="queryParams.code"
          class="!w-240px"
          clearable
          placeholder="请输入实验室编号"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search"/>
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon class="mr-5px" icon="ep:refresh"/>
          重置
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column align="center" label="实验室编号" prop="code"/>
      <el-table-column align="center" label="周次" prop="week">
        <template #default="scope">第{{scope.row.week}}周</template>
      </el-table-column>
      <el-table-column align="center" label="时间" prop="section">
        <template #default="scope">第{{(scope.row.section / 2).toFixed(0)}}节课</template>
      </el-table-column>
      <el-table-column align="center" label="星期" prop="weekDay">
        <template #default="scope">星期{{scope.row.weekDay}}</template>
      </el-table-column>
      <el-table-column :width="300" align="center" label="操作">
        <template #default="scope">
          <el-button link type="danger" @click="handleDelete(scope.row)">取消预约</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.pageNo"
      :total="total"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <RoleForm ref="formRef" @success="getList"/>
  <!-- 表单弹窗：文件上传 -->
  <FileForm ref="formUpdateRef" @success="getList"/>
</template>
<script lang="ts" setup>
import * as ProjectApi from '@/api/laboratory/lab'
import RoleForm from './RoleForm.vue'
import FileForm from './UploadForm.vue'
import {cancelAppointment} from "@/api/laboratory/lab";

defineOptions({name: 'SystemRole'})

const message = useMessage() // 消息弹窗
const {t} = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  code: ''
})
const queryFormRef = ref() // 搜索的表单

/** 查询预约列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ProjectApi.getCurrentAppointment(queryParams)
    console.log(data)
    list.value = data.result
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryParams.code = ''
  queryFormRef.value.resetFields()
  handleQuery()
}




/** 删除按钮操作 */
const handleDelete = async (row: any) => {
  console.log(row)
  try {
    // 删除的二次确认
    await message.delConfirm('是否确定取消预约')
    // 发起删除
    await ProjectApi.cancelAppointment(row)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {
  }
}




/** 初始化 **/
onMounted(async () => {
  getList()
})
</script>
